package cn.edu.gduf.luowowo.website.web.controller;


import cn.edu.gduf.luowowo.cache.service.IScenicStatisVORedisService;
import cn.edu.gduf.luowowo.comment.domain.ScenicComment;
import cn.edu.gduf.luowowo.comment.domain.TravelComment;
import cn.edu.gduf.luowowo.comment.query.ScenicCommentQuery;
import cn.edu.gduf.luowowo.comment.service.IScenicCommentService;
import cn.edu.gduf.luowowo.common.exception.LogicException;
import cn.edu.gduf.luowowo.common.util.AjaxResult;
import cn.edu.gduf.luowowo.common.util.RedisKeys;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.scenic.domain.Scenic;
import cn.edu.gduf.luowowo.scenic.domain.ScenicDetial;
import cn.edu.gduf.luowowo.scenic.query.ScenicQuery;
import cn.edu.gduf.luowowo.scenic.service.IScenicDetialService;
import cn.edu.gduf.luowowo.scenic.service.IScenicService;
import cn.edu.gduf.luowowo.website.annotation.RequiredLogin;
import cn.edu.gduf.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 景点点评控制器
 */
@Controller
@RequestMapping("/scenic")
public class ScenicController {
    @Reference
    private IScenicService scenicService;
    @Reference
    private IScenicDetialService scenicDetialService;
    @Reference
    private IScenicStatisVORedisService scenicStatisVORedisService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @RequestMapping("/list")
    public String scenicList(Model model,ScenicQuery qo) {
        //scenic 景点的分类概要
        Scenic scenic = scenicService.queryScenicIntroByDestId(qo.getDestId());
        model.addAttribute("scenic",scenic);
        //scDetials 景点的分类明细以及内部景点
        List<ScenicDetial> detials = scenicDetialService.queryTop5ByScDetialId(scenic.getId());
        model.addAttribute("scDetials",detials);
        //hotScenics 热门景点10个
        model.addAttribute("hotScenics",scenicDetialService.queryTop10ByScDetialId(scenic.getId()));
        return "scenic/list";
    }

    @RequestMapping("/allScenic")
    public String allScenic(Model model, @ModelAttribute("qo")ScenicQuery qo){
        //pageInfo 全部景点
        PageInfo pageInfo = scenicDetialService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "scenic/allScenicTpl";
    }
    @RequestMapping("/detial")
    public String scDetial(Model model,Long id,@UserParam UserInfo userInfo){
        //detial
        ScenicDetial scenicDetial = scenicDetialService.queryScDetialByDetialId(id);
        model.addAttribute("detial",scenicDetial);
        //insideScenics 内部景点
        List<ScenicDetial> scenicDetials = scenicDetialService.queryInsideScById(id);
        model.addAttribute("insideScenics",scenicDetials);

        if (userInfo!=null) {
            //查询当前用户是否收藏该景点
            String keyF = RedisKeys.SCENIC_STATIS_VO_FAVOR.join(userInfo.getId().toString());
            model.addAttribute("isFavor", scenicStatisVORedisService.queryIsFavor(keyF,id));
            //查询当前用户是否去过该景点
            String keyB = RedisKeys.SCENIC_STATIS_VO_BEEN.join(userInfo.getId().toString());
            model.addAttribute("isBeen", scenicStatisVORedisService.queryIsBeen(keyB,id));
        }
        return "scenic/detail";
    }
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long scid, @UserParam UserInfo userInfo){
        if (userInfo==null){
            AjaxResult result=new AjaxResult(false,"请登录后收藏");
            result.setCode(102);//102状态码--未登录的状态码
            return result;
        }
     boolean ret =scenicStatisVORedisService.favor(scid,userInfo.getId());
      //收藏的数量的增加和减少

     scenicDetialService.addFavorNum(scid,ret?1:-1);

        return new AjaxResult(ret).addData(scenicStatisVORedisService.getVo(scid));
    }
    @RequestMapping("/been")
    @ResponseBody
    public Object been(Long scid, @UserParam UserInfo userInfo){
        if (userInfo==null){
            AjaxResult result=new AjaxResult(false,"请登录后操作");
            result.setCode(102);//102状态码--未登录的状态码
            return result;
        }
     boolean ret =scenicStatisVORedisService.been(scid,userInfo.getId());
        //去过的数量的增加和减少

     scenicDetialService.addBeenNum(scid,ret?1:-1);

        return new AjaxResult(ret).addData(scenicStatisVORedisService.getVo(scid));
    }
    //游记新增评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(Model model, ScenicComment comment, @UserParam UserInfo userInfo) {
        if(userInfo==null){
            throw new LogicException("请登录");
        }
        comment.setAuthor(userInfo.getId());
        comment.setNickname(userInfo.getNickname());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadImgUrl(userInfo.getHeadImgUrl());
        String comId = scenicCommentService.saveOrUpdate(comment);
        model.addAttribute("userInfo",userInfo);
        //model.addAttribute("commentSon", scenicCommentService.get(comId));
        //model.addAttribute("floor", scenicCommentService.getCount(comment.getScenicId()));
        return AjaxResult.SUCCESS;//"scenic/commentTpl";
    }
    @RequestMapping("/commentList")
    public Object commentList(Model model,@ModelAttribute("qo")ScenicCommentQuery qo) {
        //查询景点明细的全部点论
        qo.setPageSize(3);
        //model.addAttribute("detail",scenicDetialService.get(qo.getScenicId()));
        Page scComments=scenicCommentService.queryCommentsByScenicId(qo);
        model.addAttribute("page",scComments);

        //model.addAttribute("list",scenicCommentService.queryByDetailId(qo.getScenicId()));
        return "scenic/scenicCommentTpl";//"scenic/commentTpl";
    }

}
